﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vroom.Common.Config;
using Vroom.Common;
using Vroom.Config;
using Vroom.DataAccessLayer.Model;
using System.Configuration;
using System.Xml.Linq;
using Vroom.Common.Enums;


namespace Vroom.Config
{
    public class ConfigFetcher : IConfigFetcher
    {

        #region Properties

        public string ConnString                    { get; set; }
        public string DTS_FILE_CONN_NAME_CONF       { get; private set; }
        public string DTS_FILE_CONN_NAME_STAGING    { get; private set; }
        public string DTS_FILE_CONN_NAME_DWH        { get; private set; }
        public string DTS_FILE_CONN_NAME_BUS        { get; private set; }
        public string FILE_SOURCE_BASE              { get; private set; }
        
        #endregion


        public ConfigFetcher()
        {
            ConnectionsDetails = new Dictionary<string, string>();
            SetConfig();
            SetConnectionDetails();
            SetConfigSource();
        }


        private void SetConfig()
        {
            DTS_FILE_CONN_NAME_CONF      = ConfigurationManager.AppSettings[Consts.SQL_ALIAS_CONF_KEY];
            DTS_FILE_CONN_NAME_STAGING   = ConfigurationManager.AppSettings[Consts.SQL_ALIAS_STAGING_KEY];
            DTS_FILE_CONN_NAME_DWH       = ConfigurationManager.AppSettings[Consts.SQL_ALIAS_DWH_KEY];
            FILE_SOURCE_BASE             = GetFileSourceLocation();
        }


        #region IConfigFetcher Members

        public Dictionary<string, string> ConnectionsDetails { get; set; }


        public string GetFileSourceLocation()
        {
            return ConfigurationManager.AppSettings[Consts.SRC_FILE_LOC];
        }


        private IEnumerable<ConnectionStringSettings> GetConnectionStrings()
        {
            List<ConnectionStringSettings> conns = new List<ConnectionStringSettings>();

            ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;

            if (settings != null)
            {
                foreach (ConnectionStringSettings cs in settings)
                {
                    conns.Add(cs);
                }
            }

            return conns;
        }


        private List<string> GetDistinctConnectionAliases()
        {
            ConfigEntities configEntities;
            List<string> distConnAliases = new List<string>();

            using (configEntities = new ConfigEntities())
            {
                distConnAliases = configEntities.VROOM_CONFIG.GroupBy(p => p.DestinationConnectionAlias)
                                                              .Select(y => y.Key)
                                                              .ToList<string>();
            }
            return distConnAliases;
        }


        public void SetConnectionDetails()
        {
            IEnumerable<ConnectionStringSettings> envConnections = GetConnectionStrings();

            foreach (ConnectionStringSettings conn in envConnections)
            {
                if (conn.Name == Consts.CONN_STR_CONF)
                { ConnectionsDetails.Add(Consts.CONN_STR_CONF, conn.ConnectionString); }

                if (conn.Name == Consts.CONN_STR_STG)
                { ConnectionsDetails.Add(Consts.CONN_STR_STG, conn.ConnectionString); }
            }
        }


        private void SetConfigSource()
        {
            ConnString = ConfigurationManager.ConnectionStrings["ConfigEntities"].ConnectionString;
        }


        public List<IConfigItem> GetItemsToLoad()
        {
            ConfigEntities configEntities;
            List<IConfigItem> lstConfigItems = null;


            using (configEntities = new ConfigEntities(ConnString))
            {
                lstConfigItems = 
                    configEntities.VROOM_CONFIG                    
                                  .ToList()
                                  .ConvertAll<IConfigItem>(p => new ConfigItem
                                  {
                                      SourceSystem                  = (SourceSystem)      Enum.Parse(typeof(SourceSystem), p.SourceSystem),
                                      SourceType                    = (SourceType)        Enum.Parse(typeof(SourceType), p.SourceType),
                                      DestinationType               = (DestinationType)   Enum.Parse(typeof(DestinationType), p.DestinationType),
                                      ColumnMappingType             = (ColumnMappingType) Enum.Parse(typeof(ColumnMappingType), p.ColumnMappingType),
                                      Source                        = GetFileLocation(p),
                                      SourceShortName               = p.Source,
                                      SourceHeaderRowCount          = p.SourceHeaderRowCount,
                                      DestinationConnectionAlias    = p.DestinationConnectionAlias,
                                      DestinationConnectionString   = ConnectionsDetails[Consts.CONN_STR_STG],
                                      MappingsOverrides             = p.MappingsOverrides,
                                      ExplicitNullColumns           = p.ExplicitNullColumns,
                                      Destination                   = p.Destination,
                                      IsEnabled                     = p.IsEnabled
                                  });
            }
            return lstConfigItems;
        }

        private string GetFileLocation(VROOM_CONFIG p)
        {
            string fileLoc = string.Empty;

            fileLoc = FILE_SOURCE_BASE + @"\" + p.SourceSystem + @"\" + p.Source;           

            return fileLoc;
        }

        #endregion
    }
}